#
#
#

srcdir          = @srcdir@
top_srcdir      = @top_srcdir@
top_builddir    = @top_builddir@
VPATH           = @srcdir@
GIT_REVISION    = @GIT_REVISION@
SVN_REVISION    = @SVN_REVISION@

PORTS           = $(shell cat ../ports.build)
ALLPORTS        = $(shell cat ../ports.all)
PORT_LIBS       = $(PORTS:%=%/port.a)

LIBS            = -lm @LIBS@

CFLAGS          = @CFLAGS@ @WALL_FLAG@
CXXFLAGS        = @CXXFLAGS@ @WALL_FLAG@
CPPFLAGS        += -I$(srcdir)
LDFLAGS         = @LDFLAGS@

ifdef SDCC_SUB_VERSION
CFLAGS          += -DSDCC_SUB_VERSION_STR=\"$(SDCC_SUB_VERSION)\"
endif

SLIBOBJS        = NewAlloc.o dbuf.o dbuf_string.o findme.o

OBJECTS         = SDCCy.o SDCChasht.o SDCCmain.o SDCCattr.o \
                  SDCCsymt.o SDCCopt.o SDCCast.o SDCCmem.o SDCCval.o \
                  SDCCicode.o SDCCbitv.o SDCCset.o SDCClabel.o \
                  SDCCBBlock.o SDCCloop.o SDCCcse.o SDCCcflow.o SDCCdflow.o \
                  SDCClrange.o SDCCptropt.o SDCCpeeph.o SDCCglue.o \
                  SDCCasm.o SDCCmacro.o SDCCutil.o SDCCdebug.o cdbFile.o SDCCdwarf2.o\
                  SDCCerr.o SDCCsystem.o SDCCgen.o

SPECIAL         = SDCCy.h version.h
OBJECTS         += SDCClex.o

SLIBSOURCES     = $(patsubst %.o,$(SLIB)/%.c,$(SLIBOBJS))
SOURCES         := $(patsubst %.o,%.c,$(OBJECTS))

# C++ sources (.cc extension)
CXXSOURCES      += $(sort $(notdir $(wildcard $(srcdir)/*.cc)))
OBJECTS         += $(patsubst %.cc,%.o,$(CXXSOURCES))

TARGET          = sdcc$(EXEEXT)

# ------------------------------------------
include $(top_builddir)/Makefile.common

# Compiling entire program or any subproject
# ------------------------------------------
all: checkconf version.h $(TARGET)

$(PORT_LIBS): FORCE 
	$(MAKE) -C $(dir $@)

FORCE:

$(top_builddir)/support/gc/libgc.a:
	$(MAKE) -C $(top_builddir)/support/gc

# Compiling and installing everything and runing test
# ---------------------------------------------------
install: all installdirs
	$(INSTALL) $(TARGET) $(DESTDIR)$(bindir)/`echo sdcc|sed '$(transform)'`$(EXEEXT)
	$(STRIP) $(DESTDIR)$(bindir)/`echo sdcc|sed '$(transform)'`$(EXEEXT)


# Deleting all the installed files
# --------------------------------
# NOTE: Deleting sdcpp in here, because the GNU cpp code base does not implement the uninstall target
uninstall:
	rm -f $(DESTDIR)$(bindir)/`echo sdcc|sed '$(transform)'`$(EXEEXT)
	rm -f $(DESTDIR)$(bindir)/`echo sdcpp|sed '$(transform)'`$(EXEEXT)

# Performing self-test
# --------------------
check:


# Performing installation test
# ----------------------------
installcheck:


# Creating installation directories
# ---------------------------------
installdirs:
	$(INSTALL) -d $(DESTDIR)$(bindir)


include $(srcdir)/clean.mk


# My rules
# --------
$(TARGET): $(SLIBOBJS) $(OBJECTS) $(PORT_LIBS)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $(SLIBOBJS) $(OBJECTS) $(PORT_LIBS) $(LIBDIRS) $(LIBS)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

$(SLIBOBJS):%.o:$(SLIB)/%.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

reswords.h: reswords.gperf Makefile
	gperf -o -k1,2,4 -t -C -N is_reserved_word $< > $@

SDCCy.h: SDCCy.c

SDCCy.c: SDCC.y
	$(YACC) -d $<
	mv y.tab.c $*.c
	mv y.tab.h $*.h

SDCClex.c: SDCC.lex SDCCy.h
	$(LEX) -8 -f -t $< >$@

# BUG: move if changed only.
version.h: ../ChangeLog Makefile
	if [ -z "${GIT_REVISION}" ]; then \
		$(AWK) -f $(srcdir)/version.awk $< > $@; \
	else \
		echo "#ifndef __VERSION_H__" > $@; \
		echo "#define __VERSION_H__" >> $@; \
		echo "// GIT_REVISION ${GIT_REVISION}" >> $@; \
		echo "// BUILD_NUMBERs are empty when not using commit from svn..." >> $@; \
		echo '#define SDCC_BUILD_NUMBER    "${SVN_REVISION}"' >> $@; \
		echo "#define SDCC_BUILD_NR        ${SVN_REVISION}" >> $@;\
		echo "#endif" >> $@; \
	fi

.l.c:
	rm -f $*.cc
	$(LEX) -t $< >$*.cc


# Remaking configuration
# ----------------------
checkconf:
	@if [ -f $(top_builddir)/devel ]; then\
	  $(MAKE) -f $(srcdir)/conf.mk srcdir="$(srcdir)" top_builddir="$(top_builddir)" \
	  freshconf;\
	fi

Makefile: $(srcdir)/Makefile.in
	 cd $(top_builddir); ./config.status src/Makefile
